package com.example.wannerone.myapplication123;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.HttpPostUtils;

import java.util.HashMap;
import java.util.Map;

public class makeuserActivity extends AppCompatActivity {

    private String result;
    Button btn_open;
    Button btn_back;
    EditText editText;
    EditText editText4;
    EditText editText3;
    EditText editText7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_makeuser);
        btn_open = (Button) findViewById(R.id.buttonforlogin);
        btn_back = (Button) findViewById(R.id.btn_back);
        editText = (EditText) findViewById(R.id.editText);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText7 = (EditText) findViewById(R.id.editText7);

        btn_open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
                Intent intent = new Intent(makeuserActivity.this, Main2Activity.class);
                //启动意图
                startActivity(intent);
            }
        });


        btn_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String usercode = editText.getText().toString();
                String username = editText4.getText().toString();
                String password = editText3.getText().toString();
                String password2 = editText7.getText().toString();
                if (!(usercode.equals("") || username.equals("") || password.equals("") || password2.equals(""))) {
                    if (password.equals(password2)) {
                        System.out.println("密码相同");
                        final Map<String, Object> userInfo = new HashMap<String, Object>();
                        userInfo.put("usercode", usercode);
                        userInfo.put("password", password);
                        userInfo.put("username",username);
                        String jsonString = JSONObject.toJSONString(userInfo);
                        final String info = "info=" + jsonString;
                        new Thread() {
                            public void run() {
                                String response = HttpPostUtils.doPostRequest("/user/register", info);
                                Message message = Message.obtain();
                                message.obj = response;
                                mHanlder.sendMessage(message);
                            }
                        }.start();


                    } else {
                        new AlertDialog.Builder(makeuserActivity.this)
                                .setTitle("系统提示")
                                .setMessage("两次输入的密码不相同")
                                .show();
                        editText.setText("");
                        editText4.setText("");
                        editText3.setText("");
                        editText7.setText("");
                    }

                } else {
                    System.out.println("输入为空");
                    //两次密码不同
                    new AlertDialog.Builder(makeuserActivity.this)
                            .setTitle("系统提示")
                            .setMessage("所有项的输入不能为空")
                            .show();
                    editText.setText("");
                    editText4.setText("");
                    editText3.setText("");
                    editText7.setText("");
                }


//                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
//                Intent intent = new Intent(makeuserActivity.this, Main2Activity.class);
//                //启动意图
//                startActivity(intent);
            }
        });
    }

    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            result = (String) message.obj;
            JSONObject resultJson = JSON.parseObject(result);
            String finalResult = resultJson.getString("result");
            System.out.println("结果是：" + finalResult);
            if (finalResult.equals("ok")) {
                System.out.println("注册成功");
                new AlertDialog.Builder(makeuserActivity.this)
                        .setTitle("系统提示")
                        .setMessage("密码修改成功！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(makeuserActivity.this,Main2Activity.class);
                                //启动意图
                                startActivity(intent);
                                makeuserActivity.this.finish();
                            }   //确定按钮的点击事件
                        })
                        .show();
//                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
//                Intent intent = new Intent(makeuserActivity.this, Main2Activity.class);
//                //启动意图
//                startActivity(intent);
            } else{
                System.out.println("注册失败");
                String detail=resultJson.getString("detail");
                if(detail.equals("account exist")){
                    new AlertDialog.Builder(makeuserActivity.this)
                            .setTitle("系统提示")
                            .setMessage("账号存在，请重新注册")
                            .show();
                }
            }
        }
    };
}
